import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:lianwenke20220712/bean/login_bean.dart';
import 'package:lianwenke20220712/http/httpUtils.dart';
import 'package:lianwenke20220712/video_Info.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// FileName 连文科
///
/// @Author/// @Date 2022/7/12 16:18
///
/// @Description 登录页面
class LoginWidget extends StatefulWidget {
  const LoginWidget({Key key}) : super(key: key);

  @override
  _LoginWidgetState createState() => _LoginWidgetState();
}

class _LoginWidgetState extends State<LoginWidget> {
  TextEditingController _userName;
  TextEditingController _pwd;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _userName = TextEditingController();
    _pwd = TextEditingController();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(

      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('密码登录',style: TextStyle(fontSize: 20),),
          TextField(
            controller: _userName,
            decoration: InputDecoration(
              hintText: '输入手机号'
            ),
          ),
          TextField(
            controller: _pwd,
            decoration: InputDecoration(
              hintText: '请输入密码'
            ),
          ),
          InkWell(
            onTap: (){
              var userName = _userName.text;
              var pwd = _pwd.text;
              HttpUtils().MendPost('http://mobile.bwstudent.com/hotNews/user/v1/login',isCache: true,map: {'userName':userName,'passwd':pwd},onSuccess: (data){
                var bean = LoginBean.fromJson(data);
                if(bean.status == '0000'){
                  Fluttertoast.showToast(msg: bean.message);
                  SpUtils(data);
                }
              });
            },
            child: Container(
              width: MediaQuery.of(context).size.width,
              height: 50,
              margin: EdgeInsets.only(top: 300),
              color: Colors.black12,
              child: Center(child: Text('登录')),
            ),
          )
        ],
      ),
    );
  }

  void SpUtils(data) async {
    var sp = await SharedPreferences.getInstance();
    sp.setBool('flag', true);
    var encode = jsonEncode(data);
    sp.setString('userName', encode);
    Navigator.of(context).pop(data);



  }
}
